﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WebUI.DAL;
using System.Data.Linq;

namespace WebUI.BL
{
    public class ProductDetails
    {
        public void Insert(tbl_ProductDetail productDetail)
        {
            using (AdlerContextDataContext contextdb = new AdlerContextDataContext())
            {
                try
                {
                    tbl_ProductDetail newproductDetail = new tbl_ProductDetail()
                    {
                        StockType=productDetail.StockType,
                        StockQuantity=productDetail.StockQuantity,
                        Status=productDetail.Status,
                        SkuCode=productDetail.SkuCode,
                        SizeNo=productDetail.SizeNo,
                        SizeName=productDetail.SizeName,
                        ProductNo=productDetail.ProductNo,
                        ProductName=productDetail.ProductName,
                        OrderQuantity=productDetail.OrderQuantity,
                        ImgSmall=productDetail.ImgSmall,
                        ImgReview=productDetail.ImgReview,
                        ImgMedium=productDetail.ImgMedium,
                        ImgLarge=productDetail.ImgLarge,
                        CreatedUser=productDetail.CreatedUser,
                        CreatedDate=productDetail.CreatedDate,
                        ColorNo=productDetail.ColorNo,
                        ColorName=productDetail.ColorName
                    };
                    contextdb.tbl_ProductDetails.InsertOnSubmit(newproductDetail);
                    contextdb.SubmitChanges();
                }
                catch (ChangeConflictException)
                {
                    //neu xay ra loi rollback lai
                    contextdb.ChangeConflicts.ResolveAll(RefreshMode.KeepCurrentValues);
                    contextdb.SubmitChanges();
                }
            }
        }
        public void Update(tbl_ProductDetail productDetail)
        {
            using (AdlerContextDataContext contextdb = new AdlerContextDataContext())
            {
                var updateproductDetail = contextdb.tbl_ProductDetails.SingleOrDefault(u => u.SkuNO == productDetail.SkuNO);
                try
                {
                    if (updateproductDetail != null)
                    {
                        updateproductDetail.StockType=productDetail.StockType;
                        updateproductDetail.StockQuantity=productDetail.StockQuantity;
                        updateproductDetail.Status=productDetail.Status;
                        updateproductDetail.SkuCode=productDetail.SkuCode;
                        updateproductDetail.SizeNo=productDetail.SizeNo;
                        updateproductDetail.SizeName=productDetail.SizeName;
                        updateproductDetail.ProductNo=productDetail.ProductNo;
                        updateproductDetail.ProductName=productDetail.ProductName;
                        updateproductDetail.OrderQuantity=productDetail.OrderQuantity;
                        updateproductDetail.ImgSmall=productDetail.ImgSmall;
                        updateproductDetail.ImgReview=productDetail.ImgReview;
                        updateproductDetail.ImgMedium=productDetail.ImgMedium;
                        updateproductDetail.ImgLarge=productDetail.ImgLarge;
                        updateproductDetail.CreatedUser=productDetail.CreatedUser;
                        updateproductDetail.CreatedDate=productDetail.CreatedDate;
                        updateproductDetail.ColorNo=productDetail.ColorNo;
                        updateproductDetail.ColorName = productDetail.ColorName;

                        contextdb.SubmitChanges();
                    }
                }
                catch (ChangeConflictException)
                {
                    //neu xay ra loi rollback lai
                    contextdb.ChangeConflicts.ResolveAll(RefreshMode.KeepCurrentValues);
                    contextdb.SubmitChanges();
                }

            }
        }
        public void Delete(int productDetailNo)
        {
            using (AdlerContextDataContext contextdb = new AdlerContextDataContext())
            {
                var deleteproductDetail = contextdb.tbl_ProductDetails.SingleOrDefault(u => u.SkuNO == productDetailNo);
                try
                {
                    if (deleteproductDetail != null)
                    {
                        contextdb.tbl_ProductDetails.DeleteOnSubmit(deleteproductDetail);
                        contextdb.SubmitChanges();
                    }
                }
                catch (ChangeConflictException)
                {
                    //neu xay ra loi rollback lai
                    contextdb.ChangeConflicts.ResolveAll(RefreshMode.KeepCurrentValues);
                    contextdb.SubmitChanges();
                }
            }
        }
        public void DeleteByStatus(int productDetailNo)
        {
            using (AdlerContextDataContext contextdb = new AdlerContextDataContext())
            {
                var deleteproductDetail = contextdb.tbl_ProductDetails.SingleOrDefault(u => u.SkuNO == productDetailNo);
                try
                {
                    if (deleteproductDetail != null)
                    {
                        deleteproductDetail.Status = "Deleted";
                        contextdb.SubmitChanges();
                    }
                }
                catch (ChangeConflictException)
                {
                    //neu xay ra loi rollback lai
                    contextdb.ChangeConflicts.ResolveAll(RefreshMode.KeepCurrentValues);
                    contextdb.SubmitChanges();
                }
            }
        }
    }
}